04. 文本 + 练习:你的首个 JOIN

编写你的首个 JOIN

以下是一个 JOIN 语句,你将有很多实践机会,没有什么学习方法比实践更强了。你将发现,我们在普通查询中引入了两个新的部分: JOIN ON JOIN 指定了你要从中获取数据的第二个表格。 ON 表示你想如何合并 FROM JOIN 语句中的表格。

SELECT orders.*
FROM orders
JOIN accounts
ON orders.account_id = accounts.id;

尝试自己运行下面的查询。

Workspace

This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity , so you may be able to download them there.

Workspace Information:

  • Default file path:
  • Workspace type: sql-evaluator
  • Opened files (when workspace is loaded): n/a

要注意什么

我们能够从两个表格中获取数据:

  1. orders
  2. accounts

我们仅从 orders 表格中获取数据。

ON 语句存储的是两个表格中相连的两列。下个部分将重点讲解这一概念。

练习问题

  1. 尝试获取 accounts 表格中的所有数据,以及 orders 表格中的所有数据。

  2. 尝试从 orders 表格中获取 standard_qty gloss_qty poster_qty ,并从 accounts 表格中获取 website primary_poc

以下是练习这两个问题的另一个数据集,你可以在下个部分检查你的答案。

Workspace

This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity , so you may be able to download them there.

Workspace Information:

  • Default file path:
  • Workspace type: sql-evaluator
  • Opened files (when workspace is loaded): n/a